<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

// 
//  index_model.php
//  www.6block.com
//  
//  Created by Will_Lee on 2011-05-17.
//  Copyright 2011 lishuzu. All rights reserved.
// 

class Index_model extends CI_model{
	function __construct(){
		parent::__construct();
		$this->load->model('Activity_model');
		$this->load->model('Goods_model');
		$this->load->model('Collection_model');
		$this->load->library('datacache');
		$this->userinfo = $this->session->userdata('userinfo');
	}
	function get_index_data(){
		$mycollections = $myaids = $activity = array();
		$mycollections = $this->Collection_model->get_Collection_by_uid($this->userinfo['uid']);
		if ($mycollections) {
			foreach ($mycollections as $key => $value) {
				$myaids[] = $value['aid'];
			}
		}
		
		
		//page
		$_SGLOBAL = $this->config->item('SGLOBAL');
		$_SGLOBAL['multipage_config']['base_url'] = base_url().'/index/index/';
		$_SGLOBAL['multipage_config']['total_rows'] = $this->Activity_model->get_act_count(1);
		$_SGLOBAL['multipage_config']['uri_segment'] = '3';
		$_SGLOBAL['multipage_config']['per_page'] = '50';
		$offset = $this->uri->segment($_SGLOBAL['multipage_config']['uri_segment']);

		$this->pagination->initialize($_SGLOBAL['multipage_config']);
		$data = array();
		
		if ($offset) {
			$activitys = $this->Activity_model->get_activitys($_SGLOBAL['multipage_config']['per_page'], $offset, 1);
		}else{
			//加载主页缓存文件
			$cachefilename = APPPATH."cache/data_index.php";
			$cachefile = '';
			$cachetime = 600;
			if(file_exists($cachefilename) && (filemtime($cachefilename) + $cachetime) > time()) {
			 	$cachefile = include_once($cachefilename);
				$activitys = $_SGLOBAL['index_data'];
			 } else {
				$this->datacache->index_cache();
				// 访客缓存
				$this->datacache->guest_cache();
				$cachefile = include_once($cachefilename);
				$activitys = $_SGLOBAL['index_data'];
			}
		}

		foreach ($activitys as $key => $activity) {
			if (!$activity['goods']) {
				$activity['goods'] = $this->Goods_model->get_goods($activity['aid'], 5);
			}
			$goods = array();
			//url格式化
			foreach ($activity['goods'] as $gid => $good) {
				$good['url'] = url_formate($good['url'], array('gid' => $good['gid']));
				$goods[] = $good;
			}
			$activity['goods'] = $goods;
			//是否已经收藏
			$activity['iscollected'] = in_array($activity['aid'], $myaids) ? 1 : 0;
			//加密
			$activity['key'] = md5($activity['aid'].'lishuzu7qiaor!@#');
			$activity['discount'] = explode('.', $activity['discount']);
			$activity['offset']['int'] = $activity['discount'][0];
			$activity['offset']['decimal'] = isset($activity['discount'][1]) ? $activity['discount'][1] : 0;
			$activity['starttime'] = date('Y-m-d H:i:s', $activity['starttime']);
			$activity['endtime'] = date('Y-m-d H:i:s', $activity['endtime']);
			$activity['dateline'] = date('Y-m-d H:i:s', $activity['dateline']);
			$activity['url'] = url_formate($activity['url'], array('aid' => $activity['aid']));
			$data['activitys'][] = $activity;
		}
		$multi = $this->pagination->create_links();
		$data['multi'] = $multi;
		return $data;
	}
}
/* End of file index.php */
/* Location: ./application/controllers/index.php */

?>